Page({
    /**
     * 页面的初始数据
     */
    data: {
      // 标签配置
      tabs: [
        { id: 0, name: "全部患者" },
        { id: 1, name: "治疗中" },
        { id: 2, name: "已完成" },
        { id: 3, name: "需随访" },
        { id: 4, name: "需复诊" },
        { id: 5, name: "待接诊" }
      ],
      currentTab: 0,
      searchKey: "",
      // 患者数据（可从接口获取）
      patients: [
        { id: 1, name: "张三", gender: "男", age: 45, lastVisit: "2023-06-15", status: 1 },
        { id: 2, name: "李四", gender: "女", age: 38, lastVisit: "2023-05-20", status: 2 },
        { id: 3, name: "王五", gender: "男", age: 52, lastVisit: "2023-06-10", status: 1 },
        { id: 4, name: "赵六", gender: "女", age: 29, lastVisit: "2023-06-05", status: 3 },
        { id: 5, name: "孙七", gender: "男", age: 35, lastVisit: "2023-06-20", status: 2 },
        { id: 5, name: "孙八", gender: "男", age: 35, lastVisit: "2023-06-20", status: 4 },
        { id: 5, name: "孙九", gender: "男", age: 35, lastVisit: "2023-06-20", status: 4 },
        { id: 5, name: "孙十", gender: "男", age: 35, lastVisit: "2023-06-20", status: 5 },
        { id: 5, name: "袁老六", gender: "男", age: 35, lastVisit: "2023-06-20", status: 5 },
      ],
      filteredPatients: [],
      // 统计数据
      totalPatients: 0,
      treatingPatients: 0,
      finishedPatients: 0,
      followupPatients: 0
    },
  
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad() {
      this.initPageData();
    },
  
    /**
     * 初始化页面数据（筛选+统计）
     */
    initPageData() {
      this.filterPatients();
      this.calculateStats();
    },
  
    /**
     * 筛选患者（标签+搜索）
     */
    filterPatients() {
      const { patients, currentTab, searchKey } = this.data;
      let result = patients;
  
      // 标签筛选
      if (currentTab !== 0) {
        result = result.filter(item => item.status === currentTab);
      }
      // 搜索筛选
      if (searchKey.trim()) {
        const key = searchKey.trim().toLowerCase();
        result = result.filter(item => item.name.toLowerCase().includes(key));
      }
  
      this.setData({ filteredPatients: result });
    },
  
    /**
     * 计算患者统计数据
     */
    calculateStats() {
      const { patients } = this.data;
      this.setData({
        totalPatients: patients.length,
        treatingPatients: patients.filter(i => i.status === 1).length,
        finishedPatients: patients.filter(i => i.status === 2).length,
        followupPatients: patients.filter(i => i.status === 3).length
      });
    },
  
    /**
     * 切换标签
     */
    switchTab(e) {
      this.setData({ currentTab: e.currentTarget.dataset.id }, () => {
        this.filterPatients();
      });
    },
  
    /**
     * 搜索输入
     */
    onSearchInput(e) {
      this.setData({ searchKey: e.detail.value });
    },
  
    /**
     * 执行搜索
     */
    onSearch() {
      this.filterPatients();
    },
  
    /**
     * 前往患者详情
     */
    gotoPatientDetail(e) {
      wx.navigateTo({ url: `/pages/patient_detail/patient_detail?id=${e.currentTarget.dataset.id}` });
    },
  
    /**
     * 前往患者档案
     */
    gotoPatientRecord(e) {
      wx.navigateTo({ url: `/pages/patient_record/patient_record?id=${e.currentTarget.dataset.id}` });
    },
  
    /**
     * 前往添加患者
     */
    gotoAddPatient() {
      wx.navigateTo({ url: '/pages/add_patient/add_patient' });
    },
  
    /**
     * 页面显示时刷新数据
     */
    onShow() {
      this.initPageData();
    },
  
    /**
     * 下拉刷新
     */
    onPullDownRefresh() {
      setTimeout(() => {
        this.initPageData();
        wx.stopPullDownRefresh();
      }, 800);
    }
  });